package com.ma.disk.controller;

import com.ma.disk.service.ObsService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;

@Controller
public class FileController {
    @Resource
    private ObsService obsService;

    @RequestMapping("/upload")
    public String fileUpload(@RequestParam("file") MultipartFile file , HttpServletRequest request) throws IOException {
        //获取文件名 : file.getOriginalFilename();
        String uploadFileName = file.getOriginalFilename();

        //如果文件名为空，直接回到首页！
        if ("".equals(uploadFileName)){
            return "redirect:/testObs.html";
        }
        System.out.println("上传文件名 : "+uploadFileName);

        //上传路径保存设置
        String path = request.getServletContext().getRealPath("/upload");
        //如果路径不存在，创建一个
        File realPath = new File(path);
        if (!realPath.exists()){
            realPath.mkdir();
        }
        System.out.println("上传文件保存地址："+realPath);
        //将路径存储到MySQL

        InputStream is = file.getInputStream(); //文件输入流
        //上传到bucket
        obsService.putOb(uploadFileName,is);


        return "testObs";
    }
    @RequestMapping("/upload2")
    public String  fileUpload2(@RequestParam("file") CommonsMultipartFile file, HttpServletRequest request) throws IOException {

        //上传路径保存设置
        String path = request.getServletContext().getRealPath("/upload");
        File realPath = new File(path);
        if (!realPath.exists()){
            realPath.mkdir();
        }
        //上传文件地址
        System.out.println("上传文件保存地址："+realPath);

        //通过CommonsMultipartFile的方法直接写文件（注意这个时候）
        file.transferTo(new File(realPath +"/"+ file.getOriginalFilename()));

        return "redirect:/testObs.html";
    }
    @RequestMapping(value="/download")
    public String downloads(HttpServletResponse response,HttpServletRequest request,String fileName) throws Exception{
        System.out.println(fileName);
        //1、设置response 响应头
        response.reset(); //设置页面不缓存,清空buffer
        response.setCharacterEncoding("UTF-8"); //字符编码
        response.setContentType("multipart/form-data"); //二进制传输数据
        //设置响应头
        response.setHeader("Content-Disposition",
                "attachment;fileName="+ URLEncoder.encode(fileName, "UTF-8"));
        //写出文件--输出流
        OutputStream out = response.getOutputStream();
        obsService.downOb(fileName,out);
        out.close();
        return null;
    }
    @RequestMapping("/share")
    public String shareFile(HttpServletResponse response, HttpServletRequest request, String fileName, Model model){
        String fileUrl = obsService.getFileUrl(fileName);
        model.addAttribute("fileUrl",fileUrl);

        return "share";
    }
}
